package com.swfcamp.shopper.ebay
{
	import com.swfcamp.shopper.IProductProvider;
	import com.swfcamp.shopper.ProductEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.HTML;

	[Event(name="com.swfcamp.shopper.ProductEvent:ProductFound", type="com.swfcamp.shopper.ProductEvent")]
	[Event(name="com.swfcamp.shopper.ProductEvent:ProductNotFound", type="com.swfcamp.shopper.ProductEvent")]

	public class EBayProductProvider extends EventDispatcher implements IProductProvider
	{
		
		private var _appId:String;
		public function get appId():String{return _appId;}
		
		public function EBayProductProvider(appId:String)
		{
			this._appId = appId;
		}

		public function search(query:String):ArrayCollection
		{
			return null;
		}
		
		public function detectProduct(html:HTML):void
		{
			var evt:ProductEvent = null;
			var itemNumber:RegExp = /item=?(\d+)/;
			var itemNum:Array = itemNumber.exec(html.location);
			
			if( itemNum != null ){
				var p:EBayProduct = new EBayProduct(itemNum[1],this);
				evt = new ProductEvent(ProductEvent.PRODUCT_FOUND,false,false,p);
			} else {
				evt = new ProductEvent(ProductEvent.PRODUCT_NOT_FOUND,false,false,null);
			}
			this.dispatchEvent(evt);
		}
		
	}
}